import http.client
import json
import mitmproxy.http
from mitmproxy import ctx
from mitmproxy.tools.main import mitmdump

class Events:
    def request(self,flow:mitmproxy.http.HTTPFlow):
        if "quote.json" in flow.request.pretty_url:
            with open("queto.json",encoding="utf-8") as f:
                flow.response = http.client.HTTPResponse.make(
                    200,
                    f.read(),
                    {"content-type":"application/json"}
                )

    def response(self,flow:mitmproxy.http.HTTPFlow):
        if "quote.json" in flow.request.pretty_url and "x=" in flow.request.pretty_url:
            ctx.log.info(str(flow.response.content))
            data = json.loads(flow.response.content)
            with open("data.json","w",encoding="utf-8") as f:
                f.write(str(data))
            data['data']['item'][0]['quote']['name'] = '比亚迪-ttttttt'
            data['data']['item'][0]['quote']['percent'] = '-0.1'
            data['data']['item'][1]['quote']['name'] = '贵州茅台-aabb'
            data['data']['item'][1]['quote']['percent'] = '0.1'
            flow.response.text = json.dumps(data)

addons = [
    Events()
]

if __name__ =='__main_':
    mitmdump(['-s',__file__,'p',"8080"])